import { supabase } from './supabase';
import { getTranslations } from '@/lib/i18n';
import { getUser } from './auth'
export interface TeamDataType {
    team_id: string;
    name: string;
    owner_id: string;
    created_at: Date;
    updated_at: Date;
    is_deleted: boolean;
    remark?: string;
};

export const getTeamData = async (team_id: string) => {
    const data: { data: TeamDataType | null, error: Error | null } = await supabase
        .from('teams')
        .select('*')
        .eq('team_id', team_id)
        .single();
    return data;
}

export const createNewTeam = async (owner_id: string, name: string) => {
    const now = new Date().toISOString();
    const data: { data: TeamDataType | null, error: Error | null } = await supabase
        .from('teams')
        .insert([
            {
                name: `${name}的团队`,
                owner_id,
                created_at: now,
                updated_at: now,
            }
        ])
        .select()
        .single();
    return data;
}

export const deleteTeam = async (team_id: string, owner_id: string) => {
    const now = new Date().toISOString();
    const data: { data: TeamDataType | null, error: Error | null } = await supabase
        .from('teams')
        .update({
            is_deleted: true,
            updated_at: now,
        })
        .eq('team_id', team_id)
        .eq("owner_id", owner_id)
        .select()
        .single();
    return data;
}
